from . import admins_bp
from .forms.site import EditForm
from flask import render_template, flash, current_app
import os


@admins_bp.route('/site', methods=['GET', 'POST'])
def site_select():
    form = EditForm()
    if form.validate_on_submit():
        with open(os.path.join(current_app.root_path, '../public/site.py'), 'w', encoding='utf-8') as f:
            for k, v in form.data.items():
                if k not in ['csrf_token', 'save']:
                    f.write("%s = '%s'\n" % (k.upper(), v))
        flash('操作成功', 'success')
    form.site_name.data = current_app.config.get('SITE_NAME')
    form.site_title.data = current_app.config.get('SITE_TITLE')
    form.site_url.data = current_app.config.get('SITE_URL')
    form.site_keyword.data = current_app.config.get('SITE_KEYWORD')
    form.site_description.data = current_app.config.get('SITE_DESCRIPTION')
    form.site_template.data = current_app.config.get('SITE_TEMPLATE')
    return render_template('site_select.html', form=form, page_title='站点信息')
